<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Login extends Controller
{

    // 登录页面
    public function index()
    {
        $admin_id = get_admin_id();
        if (!empty($admin_id)) { //已经登录
            $this->redirect(url("admin/index/index"));
        }
        return $this->fetch();
    }

    public function signup(){
        return $this->fetch();

    }

    public function forget(){
        return $this->fetch();
    }


    /**
     * 登录验证
     */
    public function login()
    {
        if (!$this->request->isPost()) {
            $this->error(lang('WRONG_REQUEST'));
        }

        $result = $this->validate($this->request->param(), 'AdminUser');
        if(true !== $result) {
            $this->error(lang('WRONG_REQUEST'));
        }
        // $captcha = $this->request->param('captcha');
        // if (empty($captcha)) {
        //     $this->error(lang('CAPTCHA_REQUIRED'));
        // }
        // //验证码
        // if (!captcha_check($captcha)) {
        //     $this->error(lang('CAPTCHA_NOT_RIGHT'));
        // }
        $name = $this->request->param('username','');
        if (empty($name)) {
            $this->error(lang('USERNAME_OR_EMAIL_EMPTY'));
        }
        $pass = $this->request->param('password','');
        if (empty($pass)) {
            $this->error(lang('PASSWORD_REQUIRED'));
        }
        // if (!check_admin_password($pass)) {
        //     $this->error(lang('PASSWORD_NOT_RIGHT'));
        // }
        
        if (strpos($name, "@") > 0) {// 邮箱登陆
            $result = Db::name('admin')
                    ->where('login_email', $name)
                    ->find();
        } else {
            $result = Db::name('admin')
                    ->where('account', $name)
                    ->find();
        }

        $user_ip = $this->request->ip();

        // $result = Db::name('admin')->where($where)->find();

        if (!empty($result)) {
            if (compare_password($pass, $result['password'])) {

                if ($result['status'] != 1) {
                    $this->error(lang('USER_DISABLED'));
                }

                // $data['ip'] = $user_ip;
                // $data['msg'] = '用户登录成功';
                // $data['username'] = $name;
                // $data['createtime'] = time();
                // Db::name('operate_log')->insert($data);

                // 更新登录信息                
                $result['login_time'] = time();
                $result['last_login_ip'] = $user_ip;
                $result['login_count'] = $result['login_count'] + 1;
                Db::name('admin')->update($result);

                session('ADMIN_ID', $result["id"]);
                session('ADMIN_USERNAME', $result["account"]);

                // 登录成功跳转
                $this->redirect('admin/index/index');
                // $this->success(lang('LOGIN_SUCCESS'));
            } else {

                // $data['ip'] = $user_ip;
                // $data['msg'] = '用户登录密码错误';
                // $data['username'] = $name;
                // $data['createtime'] = time();
                // Db::name('operate_log')->insert($data);

                $this->error(lang('PASSWORD_NOT_RIGHT'));
            }
        } else {
            // 用户不存在
            $this->error(lang('USERNAME_NOT_EXIST'));
        }
    }

    // 验证码
    public function verify(){

        $config =    [
            // 验证码字体大小
            'fontSize'    =>    60,    
            // 验证码位数
            'length'      =>    4,
        ];
        $captcha = new \think\captcha\Captcha($config);
        return $captcha->entry();
    }

    /**
     * 后台管理员退出
     */
    public function logout()
    {
        session('ADMIN_ID', null);
        session('ADMIN_USERNAME', null);

        return redirect(url('/', [], false, true));
    }
}
